//
//  KNTakePhoto.m
//  KnysSm
//
//  Created by ii on 16/9/8.
//  Copyright © 2016年 cnknys. All rights reserved.
//

#import "KNTakePhoto.h"

#define AppRootView  ([[[[[UIApplication sharedApplication] delegate] window] rootViewController] view])
#define AppRootViewController  ([[[[UIApplication sharedApplication] delegate] window] rootViewController])

#define kALERTTITLE @"设置图像"
#define kNOTSUPPORTCAMERAL @"设备不支持访问相册，请在设置->隐私->相机中进行设置！"
#define kNOTSUPPORTALBUM @"设备不支持访问相册，请在设置->隐私->照片中进行设置！"

@implementation KNTakePhoto

+ (KNTakePhoto *)sharedModel {
    static KNTakePhoto *sharedModel = nil;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken, ^{
        sharedModel = [[self alloc] init];
    });
    return sharedModel;
}

+ (void)shareViewController:(UIViewController *)controller Picture:(sendPictureBlock)block {
    
    KNTakePhoto *takePhoto = [KNTakePhoto sharedModel];
    
    takePhoto.sPictureBlock = block;
    
    UIAlertControllerStyle style;
    
    /*if (IS_IPAD) {
        style = UIAlertControllerStyleAlert;
    }else{
        style = UIAlertControllerStyleActionSheet;
    }*/
    
    style = UIAlertControllerStyleActionSheet;
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:kALERTTITLE message:nil preferredStyle: style];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        UIAlertAction *CameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *CameraAction){
            [takePhoto openCameraViewController:controller];
        }];
        [alertController addAction:CameraAction];
    }
    
    UIAlertAction *ImageAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *ImageAction){
        
        [takePhoto openAlbumViewController:controller];
    }];
    [alertController addAction:ImageAction];
    
    
    [controller presentViewController:alertController animated:YES completion:nil];

}

#pragma mark - Camera
- (void)openCameraViewController:(UIViewController *)controller {
    KNTakePhoto *takePhoto = [KNTakePhoto sharedModel];
    
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
        //无权限
        [takePhoto showAlertViewWithTitel:kNOTSUPPORTCAMERAL ViewController:controller];
        
    }else{
        // 相机 UIImagePickerControllerSourceTypeCamera
        [takePhoto setImagePickerControllerWith:UIImagePickerControllerSourceTypeCamera ViewController:controller];
    }
}

#pragma mark - Album
- (void)openAlbumViewController:(UIViewController *)controller {
    KNTakePhoto *takePhoto = [KNTakePhoto sharedModel];
    
    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
    if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied) {
        //无权限
        [takePhoto showAlertViewWithTitel:kNOTSUPPORTALBUM ViewController:controller];
        
    } else {
        // 相册 UIImagePickerControllerSourceTypePhotoLibrary,UIImagePickerControllerSourceTypeSavedPhotosAlbum
        [takePhoto setImagePickerControllerWith:UIImagePickerControllerSourceTypePhotoLibrary ViewController:controller];
    }
}

#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    KNTakePhoto *TPhoto = [KNTakePhoto sharedModel];
    
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];//获取照片的原图
    //项目修改
    //获取图片裁剪的图
    //    UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage];
    //获取图片裁剪后，剩下的图
    //    UIImage* image = [info objectForKey:UIImagePickerControllerCropRect];
    [self imageFromView:picker.view uiimage:image];
    [TPhoto sPictureBlock](image);
}

//获得某个范围内的屏幕图像
- (UIImage *)imageFromView: (UIView *)theView uiimage:(UIImage *)image
{
    UIGraphicsBeginImageContext([[UIScreen mainScreen] bounds].size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    UIRectClip(CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT));
    [theView.layer renderInContext:context];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return  image;//[self getImageAreaFromImage:theImage atFrame:r];
}


#pragma mark - showAlertView
- (void)showAlertViewWithTitel:(NSString *)title ViewController:(UIViewController *)controller{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    
    [alertController addAction:cancelAction];
    [controller presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - ImagePickerController
- (void)setImagePickerControllerWith:(UIImagePickerControllerSourceType)type ViewController:(UIViewController *)controller{
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = [KNTakePhoto sharedModel];
    imagePicker.sourceType = type;
    //设置拍照时的下方的工具栏是否显示，如果需要自定义拍摄界面，则可把该工具栏隐藏
    imagePicker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    imagePicker.allowsEditing = YES;
    [[imagePicker navigationBar] setTintColor:[UIColor whiteColor]];
    
    [controller presentViewController:imagePicker animated:YES completion:nil];
}

@end
